package views;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import android.content.ContextWrapper;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;

import com.example.android.google.apis.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

import docks.Dock;
import docks.DockController;

public class MyBalloonItemizedOverlay extends
	BalloonItemizedOverlay<OverlayItem> {

    private List<OverlayItem> items = new ArrayList<OverlayItem>();
    private Drawable markerGreen, markerOrange, markerRed, gMarkerGreen, gMarkerOrange, gMarkerRed;

    /**
     * 
     * @param defaultMarker
     * @param mapView
     * @param markers
     *            [0]->green, [1]->orange, [2]->red, [3]->glowing-green,
     *            [4]->glowing-orange, [5]->glowing-red
     */
    public MyBalloonItemizedOverlay(Drawable defaultMarker, MapView mapView,
	    Drawable[] markers) {

	super(defaultMarker, mapView);

	this.markerGreen = markers[0];
	this.markerOrange = markers[1];
	this.markerRed = markers[2];
	this.gMarkerGreen = markers[3];
	this.gMarkerOrange = markers[4];
	this.gMarkerRed = markers[5];

	List<Dock> docks = DockController.getInstance().getAllDocks();

	for (Dock d : docks) {
	    String title = d.getAddress();
	    String snippet = ""; // d.getAddress() + "\n";
	    snippet += "Available bikes: " + d.getAvailableBikes() + "\n";
	    snippet += "Free slots: " + d.getFreeSlots();

	    snippet += "@" + d.getIdDock();

	    OverlayItem item = new OverlayItem(getPoint(d.getLongitude(),
		    d.getLatitude()), title, snippet);

	    if (!d.isStarred()) {

		if (d.getAvailableBikes() != 0 && d.getFreeSlots() == 0)
		    item.setMarker(markerGreen);
		if (d.getAvailableBikes() != 0 && d.getFreeSlots() != 0)
		    item.setMarker(markerOrange);
		if (d.getAvailableBikes() == 0 && d.getFreeSlots() != 0)
		    item.setMarker(markerRed);
	    } else {

		if (d.getAvailableBikes() != 0 && d.getFreeSlots() == 0)
		    item.setMarker(gMarkerGreen);
		if (d.getAvailableBikes() != 0 && d.getFreeSlots() != 0)
		    item.setMarker(gMarkerOrange);
		if (d.getAvailableBikes() == 0 && d.getFreeSlots() != 0)
		    item.setMarker(gMarkerRed);
	    }

	    items.add(item);
	}

	populate();
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
	super.draw(canvas, mapView, shadow);
	boundCenterBottom(markerGreen);
	boundCenterBottom(markerOrange);
	boundCenterBottom(markerRed);
	boundCenterBottom(gMarkerGreen);
	boundCenterBottom(gMarkerOrange);
	boundCenterBottom(gMarkerRed);
    }

    private GeoPoint getPoint(double lat, double lon) {
	return (new GeoPoint((int) (lat * 1000000.0), (int) (lon * 1000000.0)));
    }

    @Override
    protected OverlayItem createItem(int i) {

	return (items.get(i));
    }

    @Override
    public int size() {

	return (items.size());
    }
}
